import axios from 'axios'
import storage, { TOKEN_KEY } from './../public/js/storage';

export const apiDomain = 'http://localhost:80/qs'
const baseURL = `${apiDomain}/api/v1`

// axios.defaults.baseURL = baseURL

// mock数据
// const baseURL = '/api/v1'
// const isMock = true

// 管理员登录成功后的请求需要带上token
const adminHttp = function() {
  return axios.create({
    baseURL: `${baseURL}/admin`,
    headers: {'token': storage.get(TOKEN_KEY)}
  })
}

/*---------------------------------管理员请求的接口------------------------------------*/

// 注册接口
export function register(data) {
  const url = `${baseURL}/register`
  return axios.post(url, data).then(res => Promise.resolve(res.data))
}

// 登录接口
export function login(data) {
  const url = `${baseURL}/login`
  return axios.post(url, data).then(res => Promise.resolve(res.data))
}

// 登出接口
export function logout() {
  const url = `http://localhost/qs/api/v1/admin/logout`
  return axios.post(url,{},{headers:{token:window.sessionStorage.getItem('__token__')}}).then(res => Promise.resolve(res.data))
}


// 获取问卷列表
export function getPaperList() {
  const url = '/paper-lists'
  // if (isMock) {
  //   return axios.get(url).then(res => Promise.resolve(res.data))
  // }
  return adminHttp().get(url).then(res => Promise.resolve(res.data))
}

// 删除问卷
export function deletePaper(data) {
  const url = `${baseURL}/admin/delete-paper`
  // if (isMock) {
  //   return axios.post(url, data).then(res => Promise.resolve(res.data))
  // }
  return adminHttp().post(url, data).then(res => Promise.resolve(res.data))
}

// 管理员获取问卷内容
export function getPaper(data) {
  const url = `${baseURL}/admin/view-paper`
  // if (isMock) {
  //   return axios.post(url, data).then(res => Promise.resolve(res.data))
  // }
  return adminHttp().post(url, data).then(res => Promise.resolve(res.data))
}

// 修改问卷
export function updatePaper(data) {
  const url = `${baseURL}/admin/update-paper`
  return adminHttp().post(url, data).then(res => Promise.resolve(res.data))
}

// 上传学生学籍表格
export function uploadPaper(data) {
  // axios.get('/api/data').then(res=>{
  //   console.log(res)
  // })
  // return 
  // const url = `http://localhost\/student/uploadStudents`
  const url = `${apiDomain}/student/uploadStudents`
  // const config = {
  //   headers: {
  //     'Content-Type': 'multipart/form-data'
  //   }
  // }
  return axios({
    method:'post',
    url:url,
    data:data,
    headers:{
      'Content-Type':'multipart/form-data',
      'token':window.sessionStorage.getItem('__token__')
    }
  }).then(res=>Promise.resolve(res.data))
  // return axios.post(url).then(res => Promise.resolve(res.data))
}

// 上传问卷表格
export function uploadPaper1(data) {
  // axios.get('/api/data').then(res=>{
  //   console.log(res)
  // })
  // return 
  // const url = `http://localhost\/student/uploadStudents`
  const url = `${apiDomain}/api/v1/admin/upload`
  // const config = {
  //   headers: {
  //     'Content-Type': 'multipart/form-data'
  //   }
  // }
  return axios({
    method:'post',
    url:url,
    data:data,
    headers:{
      'Content-Type':'multipart/form-data',
      'token':window.sessionStorage.getItem('__token__')
    }
  }).then(res=>Promise.resolve(res.data))
  // return axios.post(url).then(res => Promise.resolve(res.data))
}

// 获取问卷答题数据
export function getPaperData(data) {
  const url = '/paper-data'
  // if (isMock) {
  //   return axios.post(url, data).then(res => Promise.resolve(res.data))
  // }
  return adminHttp().post(url, data).then(res => Promise.resolve(res.data))
}


/*---------------------------------答题用户请求的接口------------------------------------*/

// 答题用户获取问卷内容
export function viewPaper(data) {
  const url = `${baseURL}/user/view-paper`
  return axios.get(url, data).then(res => Promise.resolve(res.data))
}

// 答题用户提交问卷答案
export function commitPaper(data) {
  const url = `${baseURL}/user/commit-paper`
  return axios.post(url, data,{headers:{'token':window.sessionStorage.getItem('stuToken')}}).then(res => Promise.resolve(res.data))
}
